import WeDisplayShaders from "./WeDisplayShaders"

export const vsSource = `
${WeDisplayShaders.WeCartesian3FromRadians}
#ifdef IS_MULTIPLE_ISO
  precision highp sampler3D;
  uniform sampler3D basePositionTexture;
  uniform sampler3D lastPositionTexture;
#else
  uniform sampler2D basePositionTexture;
  uniform sampler2D lastPositionTexture;
#endif
  uniform float isoIndexOffset;
  out vec4 v_color;
  out vec3 v_normalEC;
  out vec3 v_positionEC;
  out float v_alpha;
#ifdef IS_MULTIPLE_ISO
  vec3 WeDecodePosition(sampler3D tex,vec3 str) {
    vec3 position = texture(tex,str).xyz;
    return position;
  }
#else
  vec3 WeDecodePosition(sampler2D tex,vec3 str) {
    vec3 position = texture(tex,str.xy).xyz;
    return position;
  }
#endif

  vec3 getUV(float id,float depth)
  {
      float multiple = Weget_multiple();
      float scaleZ = Weget_scaleZ();
      float offsetZ = Weget_offsetZ();
      float bufferSize = Weget_bufferSize();
      float numVertexs = bufferSize * bufferSize;
      vec4 extent = Weget_extent();

      float row = floor( id / bufferSize);
      float col = floor(mod(id,bufferSize));
      float isoNumber = Weget_isoNumber();

      vec2 st = vec2(col / (bufferSize - 1.0),row / (bufferSize - 1.0));

      float z = depth;

      if(multiple > 1.0) {
        z /= (multiple - 1.0);
      }

      vec3 str = vec3(st,z);

      vec3 position = WeDecodePosition(basePositionTexture, str);
      bool shouldFlipY = Weget_shouldFlipY() > 0.0;
      if(shouldFlipY)
      {
        position.y = 1.0 - position.y;
      }
      position.z *= scaleZ;
      position.z += offsetZ;
      return position;
  }

  void main()
  {
      bool isLinear = Weget_isLinear() > 0.0;
      float normalFactor = Weget_normalFactor();
      float multiple = Weget_multiple();
      float meterHeight = Weget_meterHeight();
      float bufferSize = Weget_bufferSize();
      float scaleZ = Weget_scaleZ();
      float numVertexs = bufferSize * bufferSize;
      float position1 = float(gl_VertexID);
      float depth = floor(position1 / numVertexs);
      float isoValue = Weget_isoValues()[int(isoIndexOffset)];
      float isoNumber = Weget_isoNumber();
      float alpha = (isoIndexOffset + 1.0) / isoNumber;

      // alpha = pow(alpha,1.0 + isoNumber * 0.1);

      vec4 extent = Weget_customExtent();
      float vertexID = mod(position1,numVertexs);

      vec3 position = getUV(vertexID,depth);
      vec2 lonLat = WeGetImageryLonLat(position.xy,extent);
      vec3 currentWorldCoords = we_cartesian3FromRadians(vec3(lonLat, position.z * meterHeight));

      v_alpha = alpha;

      vec3 p0;
      vec3 p1;
      vec3 p2;
      float m = mod(vertexID ,3.0);
      if(m == 0.0){
        p0 = position;
        p1 = getUV(vertexID + 1.0,depth);
        p2 = getUV(vertexID + 2.0,depth);
      }
      else if(m == 1.0){
        p0 = getUV(vertexID - 1.0,depth);
        p1 = position;
        p2 = getUV(vertexID + 1.0,depth);
      }
      else if(m == 2.0){
        p0 = getUV(vertexID - 2.0,depth);
        p1 = getUV(vertexID - 1.0,depth);
        p2 = position;
      }

      p0.z *= normalFactor;
      p1.z *= normalFactor;
      p2.z *= normalFactor;

      vec3 normal = normalize(cross(p2 - p1,p0 - p1));

      float visible = 1.0;
      if(position == vec3(0.0)) visible = 0.0;
      float i = (isoValue - Weget_minValue()) / (Weget_maxValue() - Weget_minValue());
      if(!isLinear) i += 1e-6;
      v_color = texture(u_TFTexture,vec2(i,0.0));
      v_normalEC = czm_normal * normal;
      v_positionEC = (czm_modelView * vec4(currentWorldCoords,1.0)).xyz;
      gl_Position = czm_modelViewProjection * vec4(currentWorldCoords.xyz,1.0);
      gl_Position *= visible;
  }
`

export const fsSource = `
  in vec4 v_color;
  in vec3 v_normalEC;
  in vec3 v_positionEC;
  in float v_alpha;
 void main()
  {
      vec4 color = v_color;
      vec3 normalEC = v_normalEC;
      vec3 positionEC = -v_positionEC;
      czm_materialInput materialInput;
      materialInput.normalEC = normalEC;
      materialInput.positionToEyeEC = -v_positionEC;
      czm_material material = czm_getDefaultMaterial(materialInput);
      material.diffuse = color.rgb;
      material.alpha = color.a * v_alpha;
      material.specular = 0.1;
      out_FragColor = czm_phong(normalize(-v_positionEC.xyz), material, czm_lightDirectionEC);
  }
`

export default { vsSource, fsSource }
